package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gcgl.domain.SjWorkContract;
import com.ruoyi.gcgl.service.ISjWorkContractService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 劳动合同到期Controller
 * 
 * @author ruoyi
 * @date 2025-09-15
 */
@Controller
@RequestMapping("/sj/workContract")
public class SjWorkContractController extends BaseController
{
    private String prefix = "sj/workcontract";

    @Autowired
    private ISjWorkContractService sjWorkContractService;

    @RequiresPermissions("gcgl:contract:view")
    @GetMapping()
    public String contract()
    {
        return prefix + "/contract";
    }

    /**
     * 查询劳动合同到期列表
     */
    @RequiresPermissions("gcgl:contract:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SjWorkContract sjWorkContract)
    {
        startPage();
        sjWorkContract.setDeleteFlag(1);
        List<SjWorkContract> list = sjWorkContractService.selectSjWorkContractList(sjWorkContract);
        return getDataTable(list);
    }

    /**
     * 导出劳动合同到期列表
     */
    @RequiresPermissions("gcgl:contract:export")
    @Log(title = "劳动合同到期", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SjWorkContract sjWorkContract)
    {
        List<SjWorkContract> list = sjWorkContractService.selectSjWorkContractList(sjWorkContract);
        ExcelUtil<SjWorkContract> util = new ExcelUtil<SjWorkContract>(SjWorkContract.class);
        return util.exportExcel(list, "劳动合同到期数据");
    }

    /**
     * 新增劳动合同到期
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存劳动合同到期
     */
    @RequiresPermissions("gcgl:contract:add")
    @Log(title = "劳动合同到期", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SjWorkContract sjWorkContract)
    {
    	sjWorkContract.setStatus(0);
    	sjWorkContract.setDeleteFlag(1);
    	sjWorkContract.setCreateUser(getLoginName());
    	sjWorkContract.setCreateAt(new Date());
    	sjWorkContract.setUpdateUser(getLoginName());
    	sjWorkContract.setUpdateAt(new Date());
        return toAjax(sjWorkContractService.insertSjWorkContract(sjWorkContract));
    }

    /**
     * 修改劳动合同到期
     */
    @RequiresPermissions("gcgl:contract:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SjWorkContract sjWorkContract = sjWorkContractService.selectSjWorkContractById(id);
        
        mmap.put("ziwjvo", sjWorkContract.getZiwj());
        if(sjWorkContract.getZiwj()!=null && !sjWorkContract.getZiwj().equals("")){
        	String[] zlis = sjWorkContract.getZiwj().split(",");
        	String file = "";
        	for(int i=0;i<zlis.length;i++){
        		file += zlis[i]+",";
        	}
        	sjWorkContract.setZiwj(file);
        }
        
        mmap.put("sjWorkContract", sjWorkContract);
        return prefix + "/edit";
    }

    /**
     * 修改保存劳动合同到期
     */
    @RequiresPermissions("gcgl:contract:edit")
    @Log(title = "劳动合同到期", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SjWorkContract sjWorkContract)
    {
    	sjWorkContract.setUpdateUser(getLoginName());
    	sjWorkContract.setUpdateAt(new Date());
        return toAjax(sjWorkContractService.updateSjWorkContract(sjWorkContract));
    }

    /**
     * 删除劳动合同到期
     */
    @RequiresPermissions("gcgl:contract:remove")
    @Log(title = "劳动合同到期", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sjWorkContractService.deleteSjWorkContractByIds(ids));
    }
}
